import { h } from 'vue';
import { NSwitch } from 'naive-ui';
import { setRoleStatus } from '@/api/system';
import { FormSchema } from '@/components/Form';
const $message = window['$message'];
export const columns: any[] = [
	{
		title: '序号',
		key: 'orderNo',
		width: 80,
	},
	{
		title: '角色名称',

		key: 'roleName',
	},
	{
		title: '状态',
		key: 'status',
		render: (record) => {
			return h(NSwitch, {
				value: record.status === '1',
				loading: !!record.loading,
				size: 'large',
				onUpdateValue(checked: boolean) {
					record.loading = true;
					const newStatus = checked ? '1' : '0';
					setRoleStatus(record.id, newStatus)
						.then(() => {
							record.status = newStatus;
							$message.success(`已成功修改角色状态`);
						})
						.catch((err) => {
							$message.error('修改角色状态失败');
						})
						.finally(() => {
							record.loading = false;
						});
				},
			});
		},
	},
	{
		title: '创建时间',
		key: 'createTime',
	},
	{
		title: '备注',
		key: 'remark',
	},
];

export const searchFormSchema: any[] = [
	{
		field: 'deptName',
		label: '角色名称',
		component: 'NInput',
		colProps: { span: 8 },
		componentProps: {
			placeholder: '请输入角色名称',
			onInput: (e: any) => {
				console.log(e);
			},
		},
	},
	{
		field: 'status',
		label: '状态',
		component: 'NSelect',
		componentProps: {
			options: [
				{ label: '启用', value: '0' },
				{ label: '停用', value: '1' },
			],
		},
		colProps: { span: 8 },
	},
];

export const formSchema: FormSchema[] = [
	{
		field: 'roleName',
		label: '角色名称',
		component: 'NInput',
		rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
	},
	{
		field: 'status',
		label: '状态',
		component: 'NRadioGroup',
		defaultValue: '0',
		componentProps: {
			options: [
				{ label: '启用', value: '0' },
				{ label: '停用', value: '1' },
			],
		},
	},
	{
		label: '备注',
		field: 'remark',
		component: 'NInput',
		componentProps: {
			type: 'textarea',
		},
	},
	{
		label: '角色权限',
		field: 'permission',
		slot: 'permissionSlot',
	},
];
